package com.taptech.doufu.ui.adapter;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.bean.CommentDataBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.at.AtUtil;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.StartActivityUtil;
import com.taptech.doufu.ui.view.NetworkImageView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class SweepReplyAdapter extends BaseListAdapter {

	private Activity activity;
	private UserBean publishMan = null;
    private boolean novelTitleFlag;
	public SweepReplyAdapter(Activity activity) {
		this.activity = activity;
	}
    public void setNovelCurPage(boolean novelTitleFlag){
    	this.novelTitleFlag=novelTitleFlag;
    }
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = activity.getLayoutInflater().inflate(
					R.layout.sweep_comment_adapter, null);
			holder.icon = (NetworkImageView) convertView
					.findViewById(R.id.sweep_comment_user_icon);
			holder.userMedal = (ImageView) convertView
					.findViewById(R.id.sweep_comment_user_medal);
			
			holder.userName = (TextView) convertView
					.findViewById(R.id.sweep_comment_user_name);
			holder.content = (TextView) convertView
					.findViewById(R.id.sweep_comment_contents);
			holder.publishTime = (TextView) convertView
					.findViewById(R.id.sweep_comment_publishTime);
			holder.novelCurPage = (TextView) convertView
					.findViewById(R.id.novel_current_page);
			holder.novelCurPageLayout = (LinearLayout) convertView
					.findViewById(R.id.novel_current_page_layout);
			convertView.setTag(holder);
		}
		initData(convertView, position);

		return convertView;
	}

	private void initData(final View convertView, final int position) {
		try {
			publishMan = null;
			convertView.setClickable(true);
			String content = "";
			final ViewHolder holder = (ViewHolder) convertView.getTag();
			holder.icon.setImageResource(R.drawable.default_user_portrait);
			holder.icon.setRound(true);
			holder.userName.setText("");
			holder.content.setText("");

			CommentDataBean comment = null;

			comment = (CommentDataBean) getDataSource().get(position);
			publishMan = comment.getUser();
			if (publishMan == null)
				return;
			holder.icon.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					String userId = ((CommentDataBean) getDataSource().get(
							position)).getUser().getUserId();
//					Intent intent = null;
//					if (!userId.equals(AccountService.getInstance()
//							.getUserUid())) {
//						intent = new Intent(v.getContext(),
//								NewOthersPersonalActivity.class);
//						intent.putExtra(Constant.UID, userId);
//						PersonalOtherDynamicActivity.userHeader = ((ViewHolder) convertView
//								.getTag()).icon.getDrawingCache();
//					} 
//					else {
//						intent = new Intent(v.getContext(),
//								PersonalDynamicActivity.class);
//						PersonalDynamicActivity.userHeader = ((ViewHolder) convertView
//								.getTag()).icon.getDrawingCache();
//					}
//					
//					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					v.getContext().startActivity(intent);
					StartActivityUtil.startUserActivity(userId, v.getContext());
				}
			});
			content = comment.getComment_content();
			holder.publishTime.setText(DiaobaoUtil.seconds2TimeString(Long
					.parseLong(comment.getComment_time())));
			ImageManager.displayImage(holder.icon, publishMan.getIcon());
			DiaobaoUtil.setMedalImgView(publishMan.getMedal(), holder.userMedal);
			holder.userName.setText(publishMan.getName());
			new AtUtil(holder.content, content, Constant.AT_CLICK);
			//holder.content.setText(content);
			
			//显示小说评论在哪一章节
			if (novelTitleFlag&&comment.getArticle()!=null) {
				holder.novelCurPageLayout.setVisibility(View.VISIBLE);
				if (comment.getArticle()!=null&&comment.getArticle().getOrigin_title()!=null) {
					holder.novelCurPage.setText(comment.getArticle().getOrigin_title());
				}
			}else{
				holder.novelCurPageLayout.setVisibility(View.GONE);
			}
 
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	class ViewHolder {
		TextView content, userName, publishTime;
		NetworkImageView icon;
		ImageView userMedal;
		LinearLayout novelCurPageLayout;
		TextView novelCurPage;

	}

}
